package neu.soft.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import neu.soft.entity.User;
import neu.soft.service.UserService;
import neu.soft.vo.ResultBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/user")
@Api(value = "用户管理模块的API",description="用户信息相关的CRUD功能")
public class UserController {
    @Autowired
    UserService userService;

    @PostMapping("add")
    @ApiOperation(value = "添加用户信息" )
    public ResultBean add(@RequestBody User user){
       int res = userService.add(user);
          if (res==1) {
              return ResultBean.success("添加成功");
          }
          return ResultBean.error();

    }

    @PutMapping("update")
    @ApiOperation(value = "修改用户信息" )
    public ResultBean update(@RequestBody User user){
        int res = userService.update(user);
        if (res==1) {
            return ResultBean.success("修改成功");
        }
        return ResultBean.error();
    }
    @DeleteMapping("/{uid}")
    @ApiOperation(value = "根据uid删除用户信息" )
    public ResultBean delete(@PathVariable String uid){
        int res = userService.delete(uid);
        if (res==1) {
            return ResultBean.success("删除成功");
        }
        return ResultBean.error();
    }
    @PostMapping
    @ApiOperation(value = "根据名字查询用户信息" )
    public ResultBean findByName(@RequestBody Map<String, Object> map){
        Integer pageNum;
        Integer pageSize;
        pageNum = (Integer) map.get("pageNum");
        pageSize = (Integer) map.get("pageSize");
        PageHelper.startPage(pageNum,pageSize);
        String username = map.get("username").toString();

        List<User> user = userService.findByName(username);
        PageInfo<User> page = new PageInfo<>(user, 5);
        if (user!=null) {
            return ResultBean.success("查询成功",page);
        }
        return ResultBean.error();
        //之后需要发送消息，未查询到
    }

    @PostMapping("/all")
    @ApiOperation(value = "全部用户" )
    public ResultBean findAll(@RequestBody Map<String, Object> map){
        Integer pageNum;
        Integer pageSize;
        pageNum = (Integer) map.get("pageNum");
        pageSize = (Integer) map.get("pageSize");
        PageHelper.startPage(pageNum,pageSize);
        List<User> users = userService.findAll();
        PageInfo<User> page = new PageInfo<>(users, 5);
        if (users!=null) {
            return ResultBean.success("查询成功",page);
        }
        return ResultBean.error();

    }
    @PostMapping("/findById")
    @ApiOperation(value = "全部用户" )
    public ResultBean findById(@RequestBody Map<String, Object> map){


      String uid = map.get("uid").toString();
      User user = userService.findById(uid);
        if (user!=null) {
            return ResultBean.success("查询成功",user);
        }
        return ResultBean.error();

    }

}
